<?php
class Verify{
	private $char=array(
		'0'=>array(
				'',
			),
		'1'=>array(
				'',
			),
		'2'=>array(
				'000000111100001100010001000001000100000100000000100000000100000000100000000100000001100000001000010001000010001111111000',
				'000001111000011011000110011000000011000000111000000110000001100000011000000110000001100000011000000111111100',
				'001111001101101100111000001100000110000111000011000011000001100001100001100000111111',
				'000001110000100100010001000000010000001100000010000001000000100000010000001000000100000011111100',
			),
		'3'=>array(
				'000001110000111011000110011000000110000001100000011000000011100000001100000001100000001100000011000110110000011100000',
				'0011100011011011001100000110000011000011000001110000011100001110000011000011011111000011000',
				'001111000110011011000011110000110000011000011100000001100000011011000011110000110110011000111100',
				'000001111000011000010001000001000000000100000001100000111000000000010000000001000000000100010000010010000010000111110000',
				'00000110000110010001000100000010000001000000100000001100000001000000010000000100000010001001000001100000',
				'000001111000011100110001100011001100001100000001100000111100000000110000000011001100001100110000110011000110000111110000',
			),
		'4'=>array(
				'000001100000011100000111100000111100001101100001101100011001100111111111000001100000001100000001100',
				'000011000111001111001111011011011011110011110011000011000011000011',
				'000000010000001000000110000010100001010000100100010001001000100011111100000010000001000000010000',
			),
		'5'=>array(
				'000111111100010000000010000000001000000000111110000110000100000000010000000001001000001000100000100010001100000111100000',
				'00000011111000001111100000110000000011000000000111100000000011100000000011000000000110000000001100000000110000000011000001111100000000110000000',
				'0000001111000001111000001000000001000000000111000000000110000000001000000000100000000010000000010000000010000011010000000010000000',
				'111111111100000011000000110000001111110011100110000000110000001111000011110000111100011001111100',
				'001111011111011000110000111100001110000011000011000011000011000110110110011100',
			),
		'6'=>array(
				'000011001110011000011000110000111110110011110011110011110011110011011111001100',
				'001111100110001101100011110000001111110011100110110000111100001111000011110000110110011000111100',
				'000000011000001100000010000000100000001000000011111000010001000110001000100001000100011000100010000100100000011000000',
				'000001111100001100110001100011001100000000111111000111001110011000011001100001101100001100110000110011100110000111110000',
				'000001110000010001000100000001000000001011100011100110010000010010000010100000100100000100110001000011110000',
			),
		'7'=>array(
				'111111100010100010000100000100001000001000001000001000001000',
				'111111111111000011000011000110000110000110000110000110001100001100011000',
				'011111111000000110000000110000001100000001100000011000000110000000110000001100000011000000011000000110000000',
				'001111111001000010010000100000001000000010000000100000001000000001000000010000000010000000010000000100000000',
				'111111110000001100000011000001100000011000000110000011000000110000001100000011000001100000011000',
				'000111110011111001000100000001000000100000001000000100000010000000100000010000000100000010000000',
			),
		'8'=>array(
				'000011110000110011001100011001100110001110110001111100000111000011111000110011100110011000110011000110110000011100000',
				'001111000110011011000011110000110110011000111100011001100110011011000011110000110110011000111100',
				'011110110011110011110011011011011110011110111111111111110011110011110011011110',
				'00001110000100010010000100100010001100100011110000011000011010001000110010001000100010001001000001100000',
				'000001110000110001001000001001000001001000010000111100011000100010000100100000100100000100100001000011110000',
			),
		'9'=>array(
				'001111000110011011000011110000111100001101100111001111110011111100000011110001100110011000111100',
				'011110110111110011110011110011110011110011011111011111000110001110011100110000',
				'0000011110000011011000011001110001100111001110011000110001100011001100000111110000000110000000110000000110000000110000001110000000',
				'011110110111110011110011110011110011110011011111011111000110001110011100110000',
				'000111100010000101000001010000010100000110000010010001100111110000000100000010001001000011100000',
				'000001110000010010000100011000100011001100010001000010001000100000111100000001000000010000000100000001000000110000000',
				'000001111000011100110011000011001100001100110001100011001110000111111000000011000000001100110001100011001100000111100000',
			),
		'a'=>array(
				'011110011101101110110000111000011101000110110011011011110111000',
				'00111110100001000111101100011000010100001010001101111110',
				'000111100011001100110110000011100111111011001100110011001111111001100000',
				'0111111011000011000000110000001111000011110000111100011101111111',
				'0111110011000110000111100001111011000110110001101100111001111111',
				'000111000100010010010000011001110101000100100010010111100100000',
			),
		'b'=>array(
				'111000000110000001100000011111100111011001100111011000110110001101100011011001100111011000011100',
				'000011000000001100000001100000000110000000011111100011100111001100001100110000110110000110011000011001110011001111111000',
				'110000001100000011000000110000001111110011100110110000111100001111000011110000111110011011111100',
				'001110000001100000110000001111100011111101100011011000110110011111000110110011001101100001110000',
				'000010000000010000000100000000100000000111110001100011001000001001000001010000010010000010011000100101111000',
				'001100000010000010000001011000110110100001010000101000111000010100010010010000110000',
				'111000000011000000011000000011000000011111100011100110011000011011000011011000011011000011011100110011111100',
			),
		'c'=>array(
				'',
			),
		'd'=>array(
				'00000110000111100000011000000110011111100110011011000110110001101100011011000110111001100110111100111000',
				'000000001100000000010000000010000000001000011110100010000100010000010001000001001000001000100000100010000110000111111000',
				'000000111000000011000000010000111010001000110011000100010000100110001000100001000100001000110111000001000000',
				'000000110000001100000011000000110011111101100111110000111100001111000011110000110110011100111111',
				'000001110000000110000000110000000110001111110011001110110000110110000110110000110110000110011001110001111111',
				'000000000110000000001100000000110000000001100001111111000110011100011000011000110001110011000011000110000110001100111100001111110000',
				'000000111100000001110000000110000111111000110011100111001100011000110011100110001100011000110001100011111110000011000000',
			),
		'e'=>array(
				'',
			),
		'f'=>array(
				'000001111000001101110001100000000110000001111100000011000000001100000001100000000110000000011000000011000000001110000000',
				'000111110011001100110000001100001111111000110000001100000011000000110000001100000011000011111100',
				'000001110000010011000100000000100000011110000001000000001000000010000000010000000010000000100000000110000000',
				'000000011110000000110011000001100000000001100000000111111100000011000000000011000000000011000000000110000000000110000000000110000000111111000000',
				'000000011100000001000100000100000000001000000001111110000001000000000010000000000100000000010000000000100000000001000000011111000000',
			),
		'g'=>array(
				'001111111011000110011000110011000110001111100011000000001111110111000011110000011011111110',
				'000011111100010001000001000100001100110000010010000010110000011000000000111110001100001000100001000011111000001111111100',
				'0000111111001100010000100001000010001000001111000001000000000111111000100000100010000010000111110000',
				'000011111000100010000100010001100110000100100001011000011000000001111100110000100100001000111110000111110000',
				'01111111110001101100011011000110011111001100000001111110110000111100001101111110',
			),
		'h'=>array(
				'011000011100000110000011000001111110111011011001101100110110011011001101100110110011',
				'111000000001100000000110000000011000000001111111000111000110011000011001100001100110000110011000011001100001101111001111',
				'000111000000110000000110000000111111001111111001100110001100110011000110011001100011001100110011100',
				'0001100000010000000100000001011100111011001000100010001001000010010001000100010010001100',
			),
		'i'=>array(
				'0001101111001100011001110011000110011100',
				'1111111111111111',
				'01101110011001100110011001100111',
				'000111000010000010000010000100000100000100111110',
				'00011110000110000011000001100001100000110000011001111110',
				'00010010001000100100010001001000',
				'111100001100001100001100001100001100001100111111'
			),
		'j'=>array(
				'000000011000000110000000110000000110000001100000001100000001100000011000000011000111110000',
				'000000111000011110000000110000001110000001100000001100000011100000011000000011000110110000111100000',
				'00000111000000100000001000000010000001000000010000000100000010001001100011100000',
				'000011001111000011000011000011000011000011000011000011000110111110',
			),
		'k'=>array(
				'110000001100000011000000110000001100011011001100110110001101100011101100110011001100011011000011',
				'00011100000000110000000110000000011011110001101100001111100000111000000111100000011111000001101100001111111000',
				'1110000001100000011000000110111101101100011110000111000001110000011111000110110001100110',
				'000011000000001100000001100000000110000000011001110011001100001101100000111110000111011000011001100001100011001100001100',
				'000110000000010000000100000000100111000100100001011000001100000011100000010110000010010000111111000',
				'000011000000000100000000100000000010000000001001110001000100000100100000011100000011010000001000100000100010001110011100',
				'111000000011000000011000000011000000011011110011001100011011000011011000011111000011001100011000110111101111',
			),
		'l'=>array(
				'',
			),
		'm'=>array(
				'1111111011111011110110111101101111011011110110111101101111011011',
				'1111111011111011110110111101101111011011110110111101101111011011',
				'00011110110001101101000100100100010010010010010010001001001000100100101110110110',
				'0001111011100111111111001101101100011011011001101110110011011011000110110110011111111100',
				'11111111000110110110011011011001101101100110110110011011011001101101101111111111',

			),
		'n'=>array(
				'',
			),
		'o'=>array(
				'',
			),
		'p'=>array(
				'00000011110011111011000111001100011000110001100111001110011000110011100011001100011111100001100000001111000000',
				'111111100011100110011000011011000011011000011011000011011100110011111100011000000111100000',
				'00001111110000111001110001100001100011000011001100001100011000011000111001100011111110000110000000011000000000',
				'11111100111001101100001111000011110000111100001111100110111111001100000011000000',
				'00001111110000011000110000100000100001000001000100000100001000001000010001100001011100000010000000011100000000',
				'000000111001111001000110001000100001000100011001100010001000110001000100010111000010000000111000000111000000',
			),
		'q'=>array(
				'0011110001100110110001101100011011000110110001101100011001101110001111100000011000001111',
				'0001111111001100111001100001100110000110110000110011000011001100111100011111100000000110000000110000',
				'000111101001000010010000010010000010100000100100000100100001100011111000000001000000111000',
				'000111101001000110010000010010000010100000100100000100100011100011101000000001000000010000000010000',
				'000011111001110111011100110011000110111001110110001100110001100110111000011111000000011000000111000',
				'00111111011001111100001111000011110000111100001101100111001111110000001100000011',
				'000011110011001101100010010000101100011010000100100001001001100001101000000010000001100000011000',
			),
		'r'=>array(
				'011111111111011000011000011000011000011000111100',
				'00011101110000101001000011000000001000000001000000000100000000010000001111100000',
				'111101111001111011001110000001110000001100000001100000001100000111111000',
				'0001111111100001111011000011100000000110000000011000000000110000000001100000011111100000',
				'00010110111101001100000100000010000010000001000001100000',
			),
		's'=>array(
				'',
			),
		't'=>array(
				'011000111110011000011000011000011000011000011000011111001110',
				'000100001000001000111111010000010000010000100000100000100010111100',
				'00110000011000001100011111100011000001100000110000011000001100000110110001110',
				'0001000010011110010000100010000100001000100001010001000',
				'000110001100001100111111011000011000011000110000110000111100111000',
			),
		'u'=>array(
				'',
			),
		'v'=>array(
				'',
			),
		'w'=>array(
				'',
			),
		'x'=>array(
				'1111111101110110001111000011100000111000001111000110110001100110',
				'0001111111000011001000000010100000000110000000001100000000101000000010010000011101110001',
				'001110111000100100000111000000110000000110000001010000010010000100010000',
				'00111111110001101100000111100000011100000001110000001111000001101100001100110000',
				'0001111111100011101100000011110000000111000000001110000000111100000011011000011111111000',
			),
		'y'=>array(
				'',
			),
		'z'=>array(
				'',
			),
		);
	private $pic;
	public function setPicture($src){
		$this->pic=$src;
	}
	public function start(){
		$row=array();
		$src=$this->pic;
		$this->getHec($src,$row);
		$this->cleanLine($row);
		$this->cleanSpace($row);

		$record=array();
		for($v=2;;$v++){
			$x=0;$y=0;
			if(!$this->findFirstPoint($x,$y,$row)) 
				break;
			$count=0;
			$this->backtrack($x,$y,$v,$count,$row);
			$record[$v]=$count;
		}
		// $this->show($row);
		$map=array();
		$this->cut($record,$row,$map);
		for($x=0;$x<4;$x++){
			$this->cleanSpace($map[$x]);
			$this->show($map[$x]);
		}
		// show($row);
		$this->run($map);


	}

	public function getHec($imagePath,&$row) {//获取图片二进制矩阵
	    $res = imagecreatefromjpeg($imagePath);
	    $size = getimagesize($imagePath);
	    $col=array();
	    for ($i = 0; $i < $size[1]; ++$i) {
	        for ($j = 0; $j < $size[0]; ++$j) {
	            $rgb = imagecolorat($res, $j, $i);
	            $rgbarray = imagecolorsforindex($res, $rgb);
	            if ($rgbarray['red'] < 100 && $rgbarray['green']< 100 && $rgbarray['blue'] < 100) {
	                $col[$j]=1;
	            }else{
	                $col[$j]=0;
	            }
	        $row[$i]=$col;

	        }
	    }

	} 
	public function cleanLine(&$row){//去除长横线干扰

		
		for ($i = 0; $i < count($row); ++$i) {
			$count=0;	
		    for ($j = 0; $j < count($row[$i]); ++$j) {
		    	if($row[$i][$j]==1){
		    		$count++;
		    		if($count>35){//当连续的1超过35时，认为该行为干扰横线
			    		for ($j = 0; $j < count($row[$i]); ++$j) {
				    		if($row[$i-1][$j]==0&&$row[$i-1][$j]==0)
								$row[$i][$j]=0;
				    	}
				    	break;
			    	}
		    	}
		    	else{
		    		$count=0;
		    	}
	    	}

	    	
	    }
	}
	public function cleanSpace(&$row){//去除两边无效行/列（全为零行/列）
		$r=count($row);
		$c=count($row[1]);
		$n=-1;
		$finish=0;
		$temp=array();
		for ($i = 0; $i < $c; ++$i) {//去无用列
			$flag=0;
	    	for ($j = 0; $j < $r; ++$j) {
	    		if($finish||$row[$j][$i]==1){//如果该列存在1，则结束该列搜索，标记为有用列
	    			$flag=1;
	    			$n++;
	    			break;
	    		}
	    	}
	    	if($flag==1){//把有用列复制到临时数组
	    		$finish=1;
	    		for ($j = 0; $j < $r; ++$j) {
	    			$temp[$j][$n]=$row[$j][$i];
	    		}
	    	}
	    }
	    $row=$temp;
	    $temp=array();
	    //去除无用行
	    $r=count($row);
		$c=count($row[1]);
		$n=-1;
		$finish=0;
		for ($i = 0; $i < $r; ++$i) {//去无用行
			$flag=0;
	    	for ($j = 0; $j < $c; ++$j) {
	    		if($finish||$row[$i][$j]==1){//如果该行存在，则结束该行搜索，标记为有用行
	    			$flag=1;
	    			$n++;

	    			break;
	    		}
	    	}
	    	if($flag==1){//把有用行复制到临时数组
	    		$finish=1;
	    		for ($j = 0; $j < $c; ++$j) {
	    			$temp[$n][$j]=$row[$i][$j];
	    		}
	    	}
	    }
	    $row=$temp;
	    $temp=array();
	    //从右侧开始搜索最后一列有效列
	    $r=count($row);
		$c=count($row[1]);
		for ($i = $c-1; $i >=0; $i--) {
			$flag=0;
	    	for ($j = 0; $j < $r; ++$j) {
	    		if($row[$j][$i]){//找到非零数字，表示改列为最后一列有效列
	    			$flag=1;
	    			break;
	    		}
	    	}
	    	if($flag){
	    		$cc=$i;
	    		break;
	    	}

	    }
	    //echo $cc;
	    //从下方开始搜索最后一行有效行
	    for ($i = $r-1; $i >=0; $i--) {
			$flag=0;
	    	for ($j = 0; $j < $c; ++$j) {
	    		if($row[$i][$j]){//找到非零数字，表示改列为最后一行有效行
	    			$flag=1;
	    			break;
	    		}
	    	}
	    	if($flag){
	    		$rr=$i;
	    		break;
	    	}
	    }
	    //echo $rr;
	    $temp=array();
	    for($i=0;$i<=$rr;$i++)
	    	for($j=0;$j<=$cc;$j++)
	    		$temp[$i][$j]=$row[$i][$j];
	    $row=$temp;
	}
	public function cut($record,$row,&$map){
		$temp=array();
		for($i=2,$j=0;$i<count($record)+2;$i++){
			if($record[$i]<10)
				continue;
			$tx=$ty=0;
			$flag=0;//是否 找到第一个指定字符
			for($x=0;$x<count($row);$x++){
				for($y=0;$y<count($row[0]);$y++){
					if($row[$x][$y]==$i){
						$temp[$x][$y]=1;
					}else{
						$temp[$x][$y]=0;

					}
				}
			}
			$map[$j]=$temp;
			$j++;
		}
		
	    
	}
	public function show($row){
		for ($i = 0; $i < count($row); ++$i) {
		    for ($j = 0; $j < count($row[$i]); ++$j) {
		    	if($row[$i][$j]!=0)
		    		echo $row[$i][$j];
		    	else
		    		echo 0;
		    		// echo "&nbsp;";
		    }
		    // echo "<br>";
		}
		echo "<br>";
	}
	public function coder($row){
		$string="";
		for ($i = 0; $i < count($row); ++$i) {
		    for ($j = 0; $j < count($row[$i]); ++$j) {
		    	if($row[$i][$j]!=0)
		    		$string.=$row[$i][$j];
		    	else
		    		$string.=0;
		    		// echo "&nbsp;";
		    }
		}
		return $string;
	}
	public function findFirstPoint(&$x,&$y,$row){
		for ($i = $y; $i < count($row[1]); ++$i) {
		    for ($j = $x; $j < count($row); ++$j) {
		    	if($row[$j][$i]==1){
		    		$x=$j;
		    		$y=$i;
		    		return 1;
		    	}
		    }
		}
		return 0;
	}
	public function backtrack($x,$y,$v,&$count,&$row){
		$dir=array(
			array(-1,0),
			array(0,1),
			array(1,0),
			array(0,-1),
			array(-1,-1),
			array(-1,1),
			array(1,1),
			array(1,-1),
		);//标记八个方向
		// echo count($row)."A".count($row[0]);
		if($row[$x][$y]!=1||$x<0||$y<0||$x>=count($row)||$y>=count($row[0])){
			// echo "return<br>x=".$x."y=".$y."<br>";
			return;
		}

		$row[$x][$y]=$v;
		$count++;

		for($i=0;$i<8;$i++){
			$tx=$x+$dir[$i][0];
			$ty=$y+$dir[$i][1];
			
			$this->backtrack($tx,$ty,$v,$count,$row);

			
		}



	}
	function run($map){

		for($i=0;$i<4;$i++){
			$max=0;
			$percent = 0.0;
			foreach($this->char as $key =>$value){
				foreach($value as $v){
					similar_text($v, $this->coder($map[$i]), $percent);
					if(intval($percent) > $max)
					{
						$max = $percent;
						$num = $key;
						if(intval($percent) >= 100)
							break;
					}
				}
			}
			echo $num;
		}
	
	}

}